package com.woniu.smallmedical.controller;

import com.woniu.smallmedical.entity.PageBean;
import com.woniu.smallmedical.entity.ResponseResult;
import com.woniu.smallmedical.entity.StorageMessage;
import com.woniu.smallmedical.mapper.StoragemedicineMapper;
import com.woniu.smallmedical.mapper.StoragemessageMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;


@CrossOrigin
@RestController
public class StorageMessageController {

    @Autowired
    StoragemessageMapper basicmessageapper;
    @Autowired
    StoragemedicineMapper masicmessageapper;

    //分页查询
    @GetMapping("/storagemessage")
    public ResponseResult medicine(Integer pageIndex, Integer pageSize,Integer stid){
        System.out.println("id是："+stid);
        List<StorageMessage> basicmessages = basicmessageapper.selectAll(pageIndex,pageSize);
        PageBean<StorageMessage> pageBean=new PageBean<>();
        pageBean.setPageBeanList(basicmessages);
        pageBean.setPageIndex(pageIndex);
        pageBean.setPageSize(pageSize);
        pageBean.setTotalRecord(basicmessageapper.selectcount());
        return new ResponseResult<PageBean<StorageMessage>>(pageBean,200,"OK");
    }


    @GetMapping("/insertstoragemedicine")
    public void  insertmedicine(String storagepeople, String makepeoople, String type,String vendor,String number,String remark){
        System.out.println("这边接受的："+storagepeople);
        if(storagepeople.equals(null) || storagepeople==""){
            System.out.println("制单人员名字不能为空");
        }else {
            BigDecimal c   =   new   BigDecimal(Math.random()*1000+2);
            Double purchase  =c.setScale(2,   BigDecimal.ROUND_HALF_UP).doubleValue();
            BigDecimal b   =   new   BigDecimal(Math.random()*1000+1);
            Double   retail   = b.setScale(2,   BigDecimal.ROUND_HALF_UP).doubleValue();
            basicmessageapper.insertmessages(null,number,new Date(),storagepeople,type,vendor,purchase,retail,new Date(),makepeoople,remark);

        }

    }
    @GetMapping("/selectById")
    public StorageMessage  selectById(Integer stid){
        if(stid==null){
            return null;
        }else {
            System.out.println("stid是："+stid);
            StorageMessage storageMessage = basicmessageapper.selectByid(stid);
            System.out.println(storageMessage);
            return storageMessage;
        }

    }
    //分页查询名字
    @GetMapping("/storageByname")
    public ResponseResult medicineass(Integer pageIndex, Integer pageSize,String storagepeople){
        System.out.println("查询的名字是："+storagepeople);
        List<StorageMessage> basicmessages = basicmessageapper.SelctNumberName(storagepeople,pageIndex,pageSize);
        PageBean<StorageMessage> pageBean=new PageBean<>();
        pageBean.setPageBeanList(basicmessages);
        pageBean.setPageIndex(pageIndex);
        pageBean.setPageSize(pageSize);
        pageBean.setTotalRecord(basicmessageapper.selectcount());
        return new ResponseResult<PageBean<StorageMessage>>(pageBean,200,"OK");
    }

}
